package com.todofun.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import org.apache.log4j.*;

import com.todofun.business.tuanke.bo.ApplyRecordService;
import com.todofun.business.tuanke.bo.CampaignService;
import com.todofun.business.tuanke.bo.SubscribeUserService;
import com.todofun.business.tuanke.bo.CounterService;
import com.todofun.business.tuanke.vo.ApplyRecord;
import com.todofun.business.tuanke.vo.Campaign;
import com.todofun.business.tuanke.vo.Lesson;
import com.todofun.business.tuanke.vo.SubscribeUser;
import com.todofun.common.app.BaseController;
import com.todofun.common.util.CommonUtil;

@Controller
public class IndexController extends BaseController{
	private   static   final   Logger   logger   =   Logger.getLogger(IndexController.class);
	@Autowired 
	CampaignService campaignService;
	
	@Autowired 
	ApplyRecordService applyRecordService;
	
	@Autowired 
	SubscribeUserService subscribeUserService;
	
	@Autowired 
	CounterService counterService;
	
	@RequestMapping(value = "/index.do") 
	public ModelAndView showTaskListing(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  

		//读取数据库首页计数器值，并加1
		request.getSession().setAttribute("cnt", counterService.getCounter()+1);
		counterService.addCounter();		
		
		logger.warn("Remote IP: "+CommonUtil.getIpAddr(request)+"  Time:  "+ CommonUtil.getCurrentTime()); 
		
		List<Lesson> publishingCampaign = campaignService.listAllPublisingCampaign();
		modelMap.put("publishingCampaign", publishingCampaign);		
		modelMap.put("flag", "new");
		return new ModelAndView("index", modelMap);
    }
	
	@RequestMapping(value = "/pastIndex.do") 
	public ModelAndView showTaskList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {  
		List<Lesson> publishingCampaign = campaignService.listAllPublishedCampaign();
		modelMap.put("publishingCampaign", publishingCampaign);
		modelMap.put("flag", "old");
		return new ModelAndView("index", modelMap);
    }
	
	@RequestMapping(value = "/apply.do") 
	public ModelAndView showApplyForm(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String campaignId = ServletRequestUtils.getStringParameter(request,"campaignId","");
		modelMap.put("campaignId", campaignId);
		Campaign campaign = (Campaign) campaignService.getCampaignByID(Long.parseLong(campaignId));
		modelMap.put("campaign", campaign);
		return new ModelAndView("apply", modelMap);
    }
	@RequestMapping(value = "/addapply.do") 
	public ModelAndView saveAddApplyRecord(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,ApplyRecord applyRecord) throws Exception {  
		String rand = (String)request.getSession().getAttribute("rand"); 
		String input = request.getParameter("rand"); 
		if(rand.equals(input)){ 
			applyRecordService.addNewApplyRecord(applyRecord);			
			//return super.showMsg("报名已成功,请查收邮件获取团购码。<br>收到您的信息后，我们会尽快与您联系，谢谢！", "index.do");
			Campaign campaign = (Campaign) campaignService.getCampaignByID(applyRecord.getCampaignId());
			modelMap.put("campaign1", campaign);
			modelMap.put("campaignId", applyRecord.getCampaignId());
			modelMap.put("applyRecord", applyRecord);
			modelMap.put("msg", "报名已成功,请查收邮件获取团购码。<br>收到您的信息后，我们会尽快与您联系，谢谢！");
			return new ModelAndView("success", modelMap);
		} else{ 
			Campaign campaign = (Campaign) campaignService.getCampaignByID(applyRecord.getCampaignId());
			modelMap.put("campaign1", campaign);
			modelMap.put("campaignId", applyRecord.getCampaignId());
			modelMap.put("applyRecord", applyRecord);
			modelMap.put("msg", "输入的验证码有误，请重新输入！");
			return new ModelAndView("apply", modelMap);
		} 
		
    }
	
	@RequestMapping(value = "/addSubscribeUser.do") 
	public ModelAndView saveSubscribeUser(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,SubscribeUser subscribeUser) throws Exception {  
		subscribeUserService.addSubscribeUser(subscribeUser);
		return getIndex();
    }
	
	private ModelAndView getIndex(){
		return new ModelAndView("redirect:index.do");
	}

}
